package com.sport.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.sport.pojo.Account;
import com.sport.pojo.AllCourses;

import java.util.ArrayList;
import java.util.List;

public class MySQLitOpenHelper extends SQLiteOpenHelper {

    private SQLiteDatabase db = null;
    public static final String DB_NAME = "mySport.db";
    public static final String TABLE_NAME = "Account";
    public static final String TABLE_NAME_ALLCOURSES = "AllCourses";
    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
            "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "name TEXT," +
            "pwd TEXT," +
            "email TEXT," +
            "sex TEXT," +
            "pic TEXT," +
            "day INTEGER," +
            "quote TEXT," +
            "follow INTEGER," +
            "fans TEXT," +
            "collect TEXT)";
    public static final String CREATE_TABLE_COURSES = "create table " + TABLE_NAME_ALLCOURSES +
            //primary key 将id列设为主键    autoincrement表示id列是自增长的
            "(id integer primary key autoincrement," +
            "title text," +
            "isLike integer," +
            "img text)";


    public MySQLitOpenHelper(Context context) {
        super(context, DB_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        db.execSQL(CREATE_TABLE_COURSES);
    }

    public void initCoursesData() {
        System.out.println("到底调用没有");
        addCourse(new AllCourses("肩颈训练 | 韩小四", "one", 0));
        addCourse(new AllCourses("肩颈训练 | 7天速成", "two", 0));
        addCourse(new AllCourses("足弓训练 | 一周速成", "three", 0));
        addCourse(new AllCourses("全身塑形 | 30天见效", "four", 0));
        addCourse(new AllCourses("全身体态矫正 | C戈", "five", 0));
        addCourse(new AllCourses("急速瘦腿 | 周六野", "six", 0));
        addCourse(new AllCourses("驼背矫正 | 叔贵", "seven", 0));
        addCourse(new AllCourses("新手跑步 | 子章老师", "eight", 0));
        addCourse(new AllCourses("跑步干货 | 新手友好", "nine", 0));
        addCourse(new AllCourses("手臂塑形 | 海洋饼干", "ten", 0));
        System.out.println("加入啦");
    }

    //版本升级
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        // 删除旧表Account
//        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_ALLCOURSES);
//        // 删除旧表Account
//        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//        // 创建新表AllCourses
//        db.execSQL(CREATE_TABLE_COURSES);
//        // 创建新表Account，并添加新的数据字段,添加默认用户数据
//        db.execSQL(CREATE_TABLE);
////        initCoursesData(db); // 初始化AllCourses表数据
    }

    public long addAccount(Account account) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", account.getName());
        values.put("pwd", account.getPwd());
        values.put("email", account.getEmail());
        values.put("sex", account.getSex());
        //第三版，添加了默认用户数据版本
        values.put("pic", "pic1");
        values.put("day", "10");
        values.put("follow", "105");
        values.put("quote", "管住嘴迈开腿");
        values.put("fans", "122");
        values.put("collect", "1246");
        return db.insert(TABLE_NAME, null, values);

    }


    public long deleteByName(Account account) {
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(TABLE_NAME, "name like ? and pwd =?", new String[]{account.getName(), account.getPwd()});

    }

    public List<Account> selectAccountByName(Account account) {
        List<Account> accountList = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();

        Cursor cursor = db.query(TABLE_NAME, null, "name =?", new String[]{account.getName()}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                String pwd = cursor.getString(cursor.getColumnIndexOrThrow("pwd"));
                String email = cursor.getString(cursor.getColumnIndexOrThrow("email"));
                String sex = cursor.getString(cursor.getColumnIndexOrThrow("sex"));
                String pic = cursor.getString(cursor.getColumnIndexOrThrow("pic"));
                Integer day = cursor.getInt(cursor.getColumnIndexOrThrow("day"));
                String quote = cursor.getString(cursor.getColumnIndexOrThrow("quote"));
                Integer follow = cursor.getInt(cursor.getColumnIndexOrThrow("follow"));
                String collect = cursor.getString(cursor.getColumnIndexOrThrow("collect"));
                String fans = cursor.getString(cursor.getColumnIndexOrThrow("fans"));
                accountList.add(new Account(id, name, pwd, email, sex, pic, day, quote, follow, collect, fans));
            }
        }

        return accountList;
    }

    public long updateAccount(Account account) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", account.getName());
        values.put("pwd", account.getPwd());
        values.put("email", account.getEmail());
        values.put("sex", account.getSex());
        return db.update(TABLE_NAME, values, "name = ?", new String[]{account.getName()});
    }

    //    课程表相关
    public long addCourse(AllCourses courses) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", courses.getTitle());
        values.put("isLike", courses.getIsLike());
        values.put("img", courses.getImg());
        return db.insert(TABLE_NAME_ALLCOURSES, null, values);
    }

    public List<AllCourses> showAllCourses() {
        SQLiteDatabase db = getReadableDatabase();
        System.out.println("是否有用啊啊啊");
        List<AllCourses> CoursesList = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME_ALLCOURSES, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
                String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
                int isLike = cursor.getInt(cursor.getColumnIndexOrThrow("isLike"));
                String img = cursor.getString(cursor.getColumnIndexOrThrow("img"));
                CoursesList.add(new AllCourses(id, title, img, isLike));
            }
        }
        db.close();
        return CoursesList;
    }
}
